package org.qy.star_server.controller;

import org.qy.star_server.core.BrokerServer;
import org.qy.star_server.core.User;
import org.qy.star_server.data.center.DiskDataCenter;
import org.qy.star_server.data.center.MemoryDataCenter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author yinger
 * @description TODO
 * @date 2024/3/31 15:31
 **/

@RestController
@RequestMapping("/user")
public class UserController {
    private final MemoryDataCenter memory =BrokerServer.memory;

    @RequestMapping("/login")
    public boolean login(String username, String password, HttpSession session, HttpServletResponse response) {
        if (!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            return false;
        }

        User user =  memory.selectUser(username);
        if (user == null || !user.getPassword().equals(password)) {
            return false;
        }

        // 登录成功，将用户信息存入session
        session.setAttribute(username, user);
        // 将用户名存入 cookie
        Cookie cookie = new Cookie("username", username);
        cookie.setHttpOnly(false);
        response.addCookie(cookie);
        return true;
    }

    @RequestMapping("/logout")
    public boolean logout(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        Cookie[] cookies = request.getCookies();
        Cookie cookie = null;
        for (Cookie c : cookies) {
            if (c.getName().equals("username")) {
                cookie = c;
                break;
            }
        }
        // 删除session
        if (cookie != null) {
            session.setAttribute(cookie.getValue(), null);
        }
        Cookie ck = new Cookie("username", "");
        ck.setMaxAge(0);// 立即过期
        response.addCookie(ck);
        return true;
    }

    @RequestMapping("/name")
    public String getUserName(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("username")) {
                    return cookie.getValue();
                }
            }
        }
        return null;
    }


    @RequestMapping("/check")
    public boolean checkLogin(HttpServletRequest request, HttpSession session) {
        try {
            String username = getUserName(request);
            if (username == null) {
                return false;
            }
            User user = (User) session.getAttribute(username);
            if (user == null) {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }

}
